<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\CompanyShan as CompanyShanModel;
use app\common\model\Company as CompanyModel;

/**
 * 行善控制器
 * Class Companyshan
 * @package app\home\controller
 */
class Companyshan extends Base
{
    protected $moren = [
        'money' => [1,5,10,20,50,100,300,500],
    ];
    /**
     * [getprice  获取价格]
     * @param
     * @author [C。]
     * @return json
     */
    public function getprice()
    {
        $param = input('param.');
        $model = new CompanyShanModel;
        $cmodel = new CompanyModel;
        try{
            $data = $model->findData($param['id'],'comid');
            if(!empty($data)){
                $data['standard'] = unserialize($data['standard']);
            }else{
                $data = $this->moren;
            }
            $company = $cmodel->findData($param['id'],'id','mch_id,much_secret,wx_appid,wx_secret,is_son');
            if($company['is_son'] == 1){
                if(empty($company['mch_id']) || empty($company['wx_appid'])){
                    return admin_json(2, '暂未开通');
                }
            }else{
                if(empty($company['wx_appid']) || empty($company['wx_secret']) || empty($company['mch_id']) || empty($company['much_secret'])){
                    return admin_json(2, '暂未开通');
                }
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [getconfig  获取行善配置]
     * @param
     * @author [C。]
     * @return json
     */
    public function getconfig()
    {
        $param = input('param.');
        $model = new CompanyShanModel;
        try{
            $config = $model->findData($param['id'],'comid','money');
            if(empty($config)){
                $config = $this->moren;
            }else{
                $config['money'] = unserialize($config['money']);
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$config);
    }
    /**
     * [edit  编辑]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function edit()
    {
        $model = new CompanyShanModel;
        $param = input('param.');
        try{
            $gongdeng = $model->findData($param['comid'],'comid','comid');
            if(empty($gongdeng)){
                $param['standard'] = serialize($param['standard']);
                $model->addData($param);
            }else{
                $param['standard'] = serialize($param['standard']);
                $model->editDataBy($param,['comid'=>$param['comid']]);
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '编辑成功');
    }
}